package com.kaifamiao.queue;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 对应"双端队列"来说，可以参照其字符串形式来确定哪边是头哪边是尾
 * 1、在Deque中定义的操作队列的方法(不抛出异常)
 *      boolean offerFirst( E e )
 *      E peekLast()
 *      E pollLast()
 * 2、在Deque中定义的操作队列的方法(可能抛出异常)
 *      void addFirst(E e)
 *      E getLast()
 *      E removeLast()
 */
public class DequeTest2 {

    public static void main(String[] args) {

        Deque<String> d = new LinkedList<>();

        /* - - 以 字符串形式 的 右侧 为 队列头 - - */
        d.offerFirst( "刘备" );
        d.offerFirst( "刘禅" );
        System.out.println( d );

        System.out.println( d.peekLast() );
        System.out.println( d );

        System.out.println( d.pollLast() );
        System.out.println( d );

        System.out.println( d.pollLast() );
        System.out.println( d );

        System.out.println( d.peekLast() );
        System.out.println( d.pollLast() );

        System.out.println( "- ".repeat( 10 ) );

        d.addFirst( "曹操" );
        d.addFirst( "曹丕" );
        System.out.println( d );

        System.out.println( d.getLast() );
        System.out.println( d );

        System.out.println( d.removeLast() );
        System.out.println( d );

        System.out.println( d.removeLast() );
        System.out.println( d );

        // System.out.println( d.getLast() ); // NoSuchElementException
        // System.out.println( d.removeLast() ); // NoSuchElementException

    }

}
